package com.bops.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;

import com.bops.dal.user.dataobject.RenyuaninfoDO;
import com.bops.service.CommonServiceLocator;
import com.bops.service.UserService;
import com.bops.valve.webuser.BopsWebUser;
import com.yue.commons.log.LoggerFactoryWrapper;
import com.yue.commons.seine.web.annotations.ControllerAction;
import com.yue.commons.seine.web.servlet.mvc.AbstractController;
import com.yue.commons.seine.web.servlet.result.Forward;
import com.yue.commons.seine.web.servlet.result.Redirect;
import com.yue.commons.seine.web.servlet.result.View;
import com.yue.commons.seine.web.servlet.result.WebResult;

public class JoinController extends AbstractController{
	 private static Logger log = LoggerFactoryWrapper.getLogger(JoinController.class);
	 private UserService userService = CommonServiceLocator.getUserService();
	 
	 /**
	  * changePassword
	  * */
	 @ControllerAction
	 public WebResult changePassword(Map<String, Object> model,String oldPassword,
			 String newPassword){
		 BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
		  if(bopsWebUser !=null){
			  RenyuaninfoDO userDO = userService.findRenyuaninfo(bopsWebUser.getName());
			  oldPassword = (oldPassword==null)?"":oldPassword;
			  if (userDO != null)
		         {
				  if (!oldPassword.equals(userDO.getPassword()) ){
		             model.put("errorStatus", "原密码错误");
		             model.put("renInfoDO",userDO);
		             return   new View("/orderUser/show.htm");
		         	}
		         }
			  userDO.setPassword(newPassword);
			  int row =userService.updateRenyuaninfo(userDO);
			  model.put("renInfoDO",userDO);
			  model.put("errorStatus", "密码修改成功");
	          return   new View("/orderUser/show.htm");
	 	 }else{
		  return new Redirect("/login/index.htm");
	     }
		
		 
		 
	 }
	 
	 /**
	  * 修改用户信息
	  * */
	 @ControllerAction
	 public WebResult editInfo(Map<String, Object> model,RenyuaninfoDO renInfoDO){
		 int row =userService.updateRenyuaninfo(renInfoDO);
		 return show(model,"edit");
	 }
	 
	 /**
	  * 用户中心
	  * */
	 @ControllerAction
	 public WebResult show(Map<String, Object> model,String action){
		 BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
		  if(bopsWebUser !=null){
			  RenyuaninfoDO renInfoDO = userService.findRenyuaninfo(bopsWebUser.getName());
			  model.put("pagename", "个人信息");
			  model.put("renInfoDO", renInfoDO);
			  if("edit".equals(action)){
				  model.put("msg", "信息修改成功");
			  }
		  }else{
			  return new Redirect("/login/index.htm");
		  }
		  return new View("/orderUser/show.htm");
	 }
	 
	 /**
	  * 注册
	 * @throws Exception 
	  * */
	 @ControllerAction
	    public WebResult success(Map<String, Object> model) {
	        return  new Redirect("/join/view.htm");
	    }
	 
	 @ControllerAction
	 public WebResult join(Map<String, Object> model,RenyuaninfoDO renInfoDO,HttpServletRequest request) throws Exception{
		 if(renInfoDO.getEmail() !=null){
			 RenyuaninfoDO userDO = userService.findUserinfo(renInfoDO.getEmail());
			 if (userDO != null)
	         {
	             model.put("errorStatus", renInfoDO.getEmail()+"的用户已经存在，请重新输入！");
	             model.put("renInfoDO",renInfoDO);
	             return  new View("/join/join.htm");
	         }
			 renInfoDO.setEngId(renInfoDO.getEmail());
			 renInfoDO.setCompany("其它");
			 renInfoDO.setDepartment("其它");
			 userService.insertRenyuaninfo(renInfoDO);
			 request.getSession().setAttribute("bopsUserId", renInfoDO.getId());
			 model.put("status", "注册成功");
			 return new Forward("/login/success.htm");
		 }
		 
		 return new View("/join/join.htm");
	 }
	 
//	 
//	 @ControllerAction
//	 	public WebResult userCenter(Map<String, Object> model){
//		  BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
//		  if(bopsWebUser !=null){
//			  RenyuaninfoDO renInfoDO = userService.findUserinfo(bopsWebUser.getName());
//			  model.put("renInfoDO", renInfoDO);
//		  }else{
//			  return new Redirect("/index.htm");
//		  }
//		  return new Redirect("/join/view.htm");
//	 	}
	 
	 @ControllerAction
	    public WebResult loginOut(Map<String, Object> model,String username,String password,HttpServletRequest request, HttpServletResponse response) throws Exception {
	        request.getSession().setAttribute("bopsUserId",null);
	        return new  Forward("/login/index.htm");
	    }
	 
	 @ControllerAction
	    public WebResult index(Map<String, Object> model) {
	        return  new Redirect("/index.htm");
	    }
	    @ControllerAction
	    public WebResult userLogin(Map<String, Object> model,String email,String password,HttpServletRequest request) throws Exception {
	        RenyuaninfoDO renInfoDO = userService.findUserinfo(email);
	        password = (password==null)?"":password;
	        if (renInfoDO!=null)
	        { 
	            if (password.equals(renInfoDO.getPassword()) )
	            {
	            	request.getSession().setAttribute("bopsUserId", renInfoDO.getId());
	            	 return new Forward("/login/index.htm");
	            }
	        }
	        model.put("email",email);
	        model.put("errorMsg","用户名或密码不正确！");
	        return new Forward("/login/index.htm");
	    }
	    
	 

}
